package com.flashcards;

import android.graphics.Matrix;
import android.graphics.PointF;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;

public class NavigationByTouchListener implements View.OnTouchListener {
    private Navigation3D navigation;
    PointF start = new PointF();
    PointF end = new PointF();
    boolean  isMove = false;

    public NavigationByTouchListener(Navigation3D navigation) {
        this.navigation = navigation;
    }

    public boolean onTouch(View view, MotionEvent motionEvent) {

        int actionCode = motionEvent.getAction();

        Log.i("touch...", "touch happened " + actionCode);
        if (actionCode == MotionEvent.ACTION_DOWN) {
            Log.i("touch...", "touch happened Down " + motionEvent.getX());
            isMove = false;
            start.set(motionEvent.getX(), motionEvent.getY());
        }
        if (actionCode ==  MotionEvent.ACTION_MOVE) {
            Log.i("touch...", "touch happened Move " + motionEvent.getX());
            isMove = true;
        }
        if (actionCode == MotionEvent.ACTION_POINTER_UP) {
            Log.i("touch...", "touch happened up.................");
        }
        if (actionCode == MotionEvent.ACTION_UP) {
            Log.i("touch...", "touch happened Up " + motionEvent.getX());
            isMove = false;
            end.set(motionEvent.getX(), motionEvent.getY());
            if (end.x ==  start.x) {
                return false;
            }
            if (end.x - start.x > 0) {
                navigation.onKey(KeyEvent.KEYCODE_DPAD_RIGHT);
            } else {
                navigation.onKey(KeyEvent.KEYCODE_DPAD_LEFT);
            }
        }


        return true;
    }
}
